<?php

namespace App\Validators\System;

use App\Validators\PassesValidator;
use Illuminate\Support\Facades\Input;
use Prettus\Validator\Contracts\ValidatorInterface;
use Prettus\Validator\LaravelValidator;

class SystemTechValueValidator extends LaravelValidator{

    use PassesValidator;

    protected $rulesMessages=[
        'type.required' => '类型是必填的',
        'type.unique' => '该类型已经存在',
        'value.required'  => '值是必填的',];

    protected $rules = [
        ValidatorInterface::RULE_CREATE => [
            'type' => 'required|unique:system_tech_value',
            'value' => 'required'
        ],
        ValidatorInterface::RULE_UPDATE => [
            'id'   => 'required',
            'type' => 'required|unique:system_tech_value,type,{id}',
            'value' => 'required'
        ],
   ];

    public function callBackGetRules(array $rules){
        $result=[];
        $value_change = array('{id}'=>Input::get('id'));
        foreach ($rules as $key=>$value){
            if(is_string($value) && str_contains($value,'{id}')){
                $result[$key]=strtr($value,$value_change);
            }else{
                $result[$key]=$value;
            }
        }
        return $result;
    }
}
